package com.yuyou.fn.service.impl;


import java.util.Arrays;
import java.util.List;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.caucho.hessian.client.HessianProxyFactory;
import com.yuyou.fn.service.IZySmsService;
import com.yuyou.fn.util.Util;
import com.zy.entity.ZmsResult;
import com.zy.zms.ZmsService;

/**
 * Sms 表数据服务层接口实现类
 */
@Service
public class ZysmsServiceImpl implements IZySmsService {

    @Value("${dev.env}")
    private boolean dev;

    @Value("${sms.spiNo}")
    private String smsSpiNo;



    @Value("${sms.spiPassword}")
    private String smsSpiPassword;

    @Value("${check.token}")
    private String checkToken;

    @Value("${zy.sms.host}")
    private String host;
    
    @Value("${zy_sms_appid}")
    private String zy_sms_appid;
    
    @Value("${zy.sms.key}")
    private String  zy_sms_key;
    
    @Value("${zy.sms.sendDepartment}")
    private String  sendDepartment;   
    
    

    @Override
    public int sendSMS(List<String> phoneNos,String token, String content)  throws Exception{

        String s = DigestUtils.md5Hex(Util.join(phoneNos, ",") + checkToken);
        System.out.println(token);
        System.out.println(s);
        System.out.println(Util.join(phoneNos, ","));
        System.out.println(checkToken);
        if (s.equals(token)) {
            try {

                String[] phoneNoArray = phoneNos.stream().toArray(String[]::new);
                
                List<String> phoneList = Arrays.asList(phoneNoArray);
                
                String urlName = host+"/zms.hessian";  
                HessianProxyFactory factory = new HessianProxyFactory();    
                factory.setOverloadEnabled(true);  
                factory.setUser(zy_sms_appid);  
                factory.setPassword(zy_sms_key);  
                ZmsService zmsService = (ZmsService)factory.create(ZmsService.class, urlName);
                          
                JSONObject sendDepartmentJson=new JSONObject();
                sendDepartmentJson.put("sendDepartment", sendDepartment);
                
                ZmsResult result = zmsService.sendSmsBatch(content,phoneList,"卓越高中",sendDepartmentJson.toJSONString());
                
                System.out.println(result.getCode());
                
                if(result.getCode().equals("0")){
                	return 0;
                }
                return 3;
                         
                
            } catch (Exception e) {
                System.out.println(e.getMessage());
                throw e;
            }
        } else {
            return 3;
        }

    }

}